#include "CEnemy.h"
#include "Globals.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "Messaging\EventSystem.h"

CEnemy::CEnemy()
{
	SetRotation(0.0f);
	/*SetPosX(0);
	SetPosY(0);*/
	SetVelX(0);
	SetVelY(0);
	SetWidth(29);
	SetHeight(32);
	SetScale(1.0f);
//	SetRotation(0);
	
	SetImageID(ENEMY_SELF, CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/Link Shield.png"));
	SetImageID(ENEMY_HEALTH_BAR, CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/StS_bar 3 B.png"));
	SetImageID(ENEMY_BAR_BORDER, CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/bar border.png"));
	
	EventSystem::GetInstance()->RegisterClient(REDUCE_ENEMY_HEALTH, this);
}

CEnemy::~CEnemy()
{
	EventSystem::GetInstance()->UnregisterClient(REDUCE_ENEMY_HEALTH, this);
}

void CEnemy::Update( float fElapsedTime )
{
	
}

void CEnemy::Render( void )
{
	CSGD_TextureManager::GetInstance()->Draw(GetImageID(ENEMY_SELF), GetPosX(), GetPosY(), GetScale(), GetScale(), 0, (float)(GetWidth() >> 1), (float)(GetHeight() >> 1), GetRotation());

	CSGD_TextureManager::GetInstance()->Draw(GetImageID(ENEMY_BAR_BORDER), GetPosX(), GetPosY() - 25, 0.4f, GetScale() * 0.5f);
	CSGD_TextureManager::GetInstance()->Draw(GetImageID(ENEMY_HEALTH_BAR), GetPosX(), GetPosY() - 24, (((float)m_nHealth / (float)m_nMaxHealth) * 0.4f), GetScale() * 0.5f);
}

void CEnemy::Reset()
{

}

bool CEnemy::CheckCollision( IBaseInterface* pBase )
{
	return false;
}

void CEnemy::HandleEvent(CEvent* pEvent)
{
	if(pEvent->GetEventID() == REDUCE_ENEMY_HEALTH )
	{
		SetPosY(GetPosY() + 25);
	}
}

// updates the enemy's position to home in on a target
void CEnemy::UpdateSeek()
{
	//tSteeringOutput steering;

	////the position of the homing bullet
	//tVector2D myPosition;	
	//myPosition.fX = GetPosX();
	//myPosition.fY = GetPosY();
	//tVector2D myVelocity;
	//myVelocity.fX = GetVelX();
	//myVelocity.fY = GetVelY();

	////the position of the player's ship
	//tVector2D targetPosition;					
	//targetPosition.fX = m_pTarget->GetPosX() + 20;
	//targetPosition.fY = m_pTarget->GetPosY() + 20;

	//float targetSpeed = 0;
	//tVector2D targetVelocity;

	//// Get the direction to the target
	//tVector2D direction = targetPosition - myPosition;
	//float distance = Vector2DLength(direction);

	////if we have reached the target, return 
	//if (distance < GetTargetRadius() )
	//	return;

	////if we are outside the slowRadius, then go max speed
	//if (distance > GetSlowRadius() )
	//{
	//	targetSpeed = GetMaxSpeed();
	//}
	//// otherwise calculate a scaled speed 
	//else
	//{
	//	targetSpeed = GetMaxSpeed() * distance / GetSlowRadius();
	//}

	//// The target velocity combines speed and direction
	//targetVelocity = direction;
	//targetVelocity = Vector2DNormalize(targetVelocity);
	//targetVelocity = targetVelocity * targetSpeed;

	//// Acceleration tries to get to the target velocity
	//steering.linear = targetVelocity - myVelocity;
	//steering.linear = steering.linear / GetTimeToTarget();

	//// Check if the acceleration is too fast
	//if (Vector2DLength(steering.linear) > GetMaxAcceleration() )
	//{
	//	steering.linear = Vector2DNormalize(steering.linear);
	//	steering.linear = steering.linear * GetMaxAcceleration();
	//}

	////output the steering
	//steering.angular = 0;
	//SetVelX(steering.linear.fX);
	//SetVelY(steering.linear.fY);
}
